package com.mashiro.system.controller;

import com.mashiro.common.annotation.Log;
import com.mashiro.common.core.controller.BaseController;
import com.mashiro.common.core.domain.AjaxResult;
import com.mashiro.common.core.domain.entity.SysRole;
import com.mashiro.common.core.domain.model.LoginUser;
import com.mashiro.common.core.page.TableDataInfo;
import com.mashiro.common.enums.BusinessType;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.common.utils.poi.ExcelUtil;
import com.mashiro.system.domain.MoeMaterial;
import com.mashiro.system.mapper.MoeProjectMapper;
import com.mashiro.system.service.IMoeMaterialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 材料管理Controller
 *
 * @author mashiro
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/system/material")
public class MoeMaterialController extends BaseController {
    @Resource
    private IMoeMaterialService moeMaterialService;

    @Resource
    private MoeProjectMapper moeProjectMapper;

    /**
     * 查询材料管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoeMaterial moeMaterial) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        AtomicBoolean isAdmin = new AtomicBoolean(false);
        AtomicBoolean isManager = new AtomicBoolean(false);
        List<MoeMaterial> list = new ArrayList<>();
        startPage();
        roles.forEach(sysRole -> {
            if (11L == sysRole.getRoleId()){
                isManager.set(true);
            }else if (1L == sysRole.getRoleId() || 10L == sysRole.getRoleId()){
                isAdmin.set(true);
            }
        });
        if (!isAdmin.get()){
            if (isManager.get()){
                Long[] projectIds = moeProjectMapper.selectMoeProjectIdsByManagerId(SecurityUtils.getUserId());
                list = moeMaterialService.getMaterialListByProjectIds(moeMaterial, projectIds);
            }
        }else {
            list = moeMaterialService.selectMoeMaterialList(moeMaterial);
        }
        return getDataTable(list);
    }

    /**
     * 导出材料管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:material:export')")
    @Log(title = "材料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoeMaterial moeMaterial) {
        List<MoeMaterial> list = moeMaterialService.selectMoeMaterialList(moeMaterial);
        ExcelUtil<MoeMaterial> util = new ExcelUtil<MoeMaterial>(MoeMaterial.class);
        util.exportExcel(response, list, "材料管理数据");
    }

    /**
     * 获取材料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:material:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(moeMaterialService.selectMoeMaterialById(id));
    }

    /**
     * 新增材料管理
     */
    @PreAuthorize("@ss.hasPermi('system:material:add')")
    @Log(title = "材料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoeMaterial moeMaterial) {
        return toAjax(moeMaterialService.insertMoeMaterial(moeMaterial));
    }

    /**
     * 修改材料管理
     */
    @PreAuthorize("@ss.hasPermi('system:material:edit')")
    @Log(title = "材料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoeMaterial moeMaterial) {
        return toAjax(moeMaterialService.updateMoeMaterial(moeMaterial));
    }

    /**
     * 删除材料管理
     */
    @PreAuthorize("@ss.hasPermi('system:material:remove')")
    @Log(title = "材料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(moeMaterialService.deleteMoeMaterialById(id));
    }
}
